/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

self.AnnotationBrowser = function(channel_list,container,editable)
{
	this.el_main = Element("div",{className:"annotation_browser"},container)
	
	this.channels = new Array();
	this.channel_list = channel_list;
	for(var i=0;i<channel_list.length;i++)
	{
		view = new ChannelView(channel_list[i],this.el_main,editable);
		this.channels.push(view);
	}	
	
	$(this.el_main).accordion({ icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' } })
	
	$(document).bind("annotation_done", bind(this,"onAnnotationDone"));
	
}
AnnotationBrowser.prototype.highlightAnnotations = function(playlist_item,time)
{
	for(var i=0;i<this.channels.length;i++)
	{
		channel_view = this.channels[i];
		channel_view.highlightAnnotations(playlist_item,time);
	}
}
AnnotationBrowser.prototype.showChannel = function(channel_view)
{
	for(var i=0;i<this.channels.length;i++)
	{
		c = this.channels[i]

		if(channel_view == c)
		{
			c.slideOpen()
		}
	}	
}
AnnotationBrowser.prototype.onAnnotationDone = function(evt,annotation,channel)
{
	channel_view = this.channels.findObj("channel",channel)
	channel_view.refresh();
	
	this.showChannel(channel_view)
	
	if(annotation)
	{
		channel_view.showItem(annotation);
	}
}

ChannelView = function(channel,container,editable)
{
	
	//
	this.channel = channel;
	this.editable = editable
		
	//channel header
	this.el_channel_header = Element("div",{className:"channel_view"},container)
	el_channel_title = Element("h3",{className:"cv_channel_title"},this.el_channel_header,channel.name)
	el_channel_color_dot = Element("div",{className:"color_dot ui-corner-all"},this.el_channel_header)
	el_channel_color_dot.style.backgroundColor = this.channel.getColor()
	
	//
	this.el_channel_content = Element("div",{},container)
		
	this.upateAnnotationList()
	
	
}
ChannelView.prototype.upateAnnotationList = function()
{	
	this.channel.annotations.sortBy("time");
	
	$(this.el_channel_content).empty();
	el_channel_item_list = Element("ul",{className:"channel_item_list"},this.el_channel_content);
	
	for(var i=0;i<this.channel.annotations.length;i++)
	{
		annotation = this.channel.annotations[i]
		
		el_list_item =  Element("li",{className:"ab_list_item"},el_channel_item_list)
		el_list_item.annotation = annotation; //need to find the li element back by its annotation
		el_annotation = Element("span",{},el_list_item);
		//el_img = Element("img",{src:"http://" + document.location.host + "/frame/icon/" + annotation.movieId + "/" + annotation.time},el_annotation);
		el_time = Element("span",{className:"ab_time ui-corner-all"},el_annotation,AnnotraxTime.secondsToTimeString(annotation.getBeginTime()));
		el_text = Element("span",{className:"ab_text"},el_annotation,annotation.toText(20));
		if(this.editable)
		{
			el_edit = Element("a",{className:"ab_modify"},el_annotation,"modify");
		}
		el_list_item.addEventListener("click",bind(this,"onClick",[this.channel,annotation]),true);
	}
}
ChannelView.prototype.onClick = function(channel,annotation,evt)
{
	$(document).trigger(jQuery.Event("annotation_click"),[channel,annotation]);
	this.showItem(annotation);
}

ChannelView.prototype.refresh = function()
{
	this.upateAnnotationList()
}
ChannelView.prototype.slideOpen = function()
{
	$(this.el_channel_header).trigger("click");		
}
ChannelView.prototype.showItem = function(annotation)
{
	el_to_scroll = null
	
	list = $("li",this.el_channel_content)
	list.removeClass('selected')
	for(var i=0;i<list.length;i++)
	{
		el_to_scroll = list[i];
		if(el_to_scroll.annotation == annotation)
		{
			$(el_to_scroll).addClass('selected')
			break;
		}
	}
	
	$("ul",this.el_channel_view).scrollTo(el_to_scroll)
}
ChannelView.prototype.highlightAnnotations = function(playlist_item,time)
{		
    for(var i=0;i<this.el_channel_item_list.childNodes.length;i++)
	{
		el_li = this.el_channel_item_list.childNodes[i];
		el_li.className = "";
		if(playlist_item.id != null) //playlist player is in playlist mode 
		{
			if(playlist_item.id == el_li.annotation.id)
			{
				el_li.className = "now_playing";
			}
		}
		else //playlist player is in single movie mode
		{
			if((time >= el_li.annotation.getBeginTime()) && (time <= el_li.annotation.getEndTime()) )
			{
				el_li.className = "now_playing";
			}
		}
	}
}